/*************************************************
 *FileName:      forgetpsd.js
 *Description:   忘记密码.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    window.addEventListener('rssecurity', function(e){//执行刷新
        location.reload();
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var that = getUrlParam('that');
    var code_mobile = '';
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        if(that=='login'){
            $('.mui-title').text('重置登陆密码');
            $('.d-question').css({display:'none'})
            $('.d-key').css({display:'none'})
        }else{
            $('.mui-title').text('设置手势密码');
            state();
        }
        
        $('.mui-popover').on('tap','.mui-table-view-cell',function(){
            $('.question').val($(this).find('a').text()) ;
             question=$(this).attr('data-id');
            $('.mui-popover').removeClass('mui-active');
            $('.mui-popover').css({display:'none'});
            $('.mui-backdrop').remove();
        })

        //发送手机验证码
        $('.getcode').on('tap',function(){
            var mobile = $("input[name='mobile']").val();
            var answer = $("input[name='answer']").val();
            code_mobile = mobile;
            if(mobile == '') {
                $("input[name='mobile']").attr("placeholder", "请输入手机号！");
                return false
            }
            if(mobile == '' || !/^1[3456789]\d{9}$/.test(mobile)) {
                mui.toast('请输入正确的手机号码',{duration:2000,type:'div'});
                return;
            }
            var btnID = this;
            var times = 60;
            btnID.setAttribute("disabled","disabled");
            btnID.innerHTML = times+"s后重新发送";
            var outtime = setInterval(function(){
                times--;
                btnID.innerHTML = times+"s后重新发送";
                if(times<=0){
                    clearInterval(outtime);
                    btnID.innerHTML = "发送验证码";
                    btnID.removeAttribute("disabled");
                }
            },1000);
            
            
            if(that=='login'){
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/code/passwordResetCode",
                    dataType:'jsonp',
                    data: { uid:uid,token:token,mobile: mobile ,question: question, answer: answer},
                    success: function(data) {
                        if(data.error_code==0){
                            console.log(data)
                            localStorage.replace_reset_code = data.code;
                        }else{
                            mui.toast(data.error_msg)
                            clearInterval(outtime);
                            btnID.innerHTML = "发送验证码";
                            btnID.removeAttribute("disabled");
                        }
                    }
                });
            }else{
                
                if(question == '' ) {
                    mui.toast('请选择保密问题！',{duration:2000,type:'div'});
                    return;
                }
                if(answer == '' ) {
                    mui.toast('请输入保密答案！',{duration:2000,type:'div'});
                    return;
                }
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/code/turesPasswordResetCode",
                    dataType:'jsonp',
                    data: { uid:uid,token:token,mobile: mobile ,question: question, answer: answer},
                    success: function(data) {
                        if(data.error_code==0){
                            localStorage.replace_reset_code = data.code;
                        }else{
                            mui.toast(data.error_msg)
                            clearInterval(outtime);
                            btnID.innerHTML = "发送验证码";
                            btnID.removeAttribute("disabled");
                        }
                        console.log(data)
                    }
                });
            }
                
        })
        $.ajax({//加载问题
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/forgetPwd",
            dataType:'jsonp',
            data: {uid:uid,token:token},
            success: function(data) {
                if(data.error_code == 0) {
                    var oli="";
                    for(var i=0;i<data.data.length ; i++){
                        oli+='<li class="mui-table-view-cell" data-id="'+data.data[i].id+'"><a>'+data.data[i].content+'</a></li>';
                    }
                    $('#middlePopover .mui-table-view').html(oli);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        });
        $('.rdntn').on('tap',function(){
            var mobile = $("input[name='mobile']").val();
            var code = $("input[name='code']").val();
            var answer = $("input[name='answer']").val();
            if(!question){
                question=0;
                answer="";
            }
            var codes = localStorage.replace_reset_code;
                if(code == '') {
                    // mui.alert('请输入验证码！','提示','确定',function(){},'div');
                    mui.toast('请输入验证码！',{ duration:2000, type:'div' });
                    return false
                }
                if(!codes){
                    mui.toast('请先获取验证码！',{ duration:2000, type:'div' });
                    return false
                }
                if(mobile != code_mobile) {
                    $("input[name='mobile']").val('');
                    // mui.alert('手机号码不正确！','提示','确定',function(){},'div');
                    mui.toast('手机号码必须填写获取验证码的号码！',{ duration:2000, type:'div' });
                    return false
                }
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/user/answerCheck",
                    dataType:'jsonp',
                    data: { mobile: mobile, code: code, codes: codes, question: question, answer: answer },
                    success: function(data) {
                        if(data.error_code == 0) {
                            if(that){
                                localStorage.removeItem('replace_reset_code');
                                localStorage.replace_reset_mobile = mobile;
                                common.pageTransition("resetgesturecode.html?that=login")
                            }else{
                                common.pageTransition("resetgesturecode2.html")
                            }
                            
                        }else{
                            mui.toast(data.error_msg,{ duration:2000, type:'div' });
                        }
                    }
                });
                
        })
    }
    /**
     * 判断用户状态 
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function state(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/usersAnswer",
            dataType:'jsonp',
            data: { uid: uid, token: token,},
            success: function(data) {
                if(data.error_code == 0){
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    setTimeout(function(){
                        mui.confirm('您还未设置密保问题，请设置密保问题！','提示',['设置','取消'],function(w){
                            if(w.index==0 ){
                                setTimeout(function(){common.pageTransition('security.html'),500});
                            }else{
                                setTimeout(function(){mui.back(),500});
                            }
                        },'div')
                    },500);
                }
            }
        }); 
    }
    /**
     * 密保问题
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function question(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/securityQuestion",
            dataType:'jsonp',
            data: { uid: uid, token: token,},
            success: function(data) {
                if(data.error_code == 0){
                    var ohtml='';
                    for(var i=0 ; i<data.data.length;i++){
                        ohtml+='<li class="mui-table-view-cell" data-id="'+data.data[i].id+'"><a>'+data.data[i].content+'</a></li>'
                    }
                    $('.mui-scroll-wrapper .mui-table-view').html(ohtml);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        }); 
    }
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */